package org.egovframeex.mybatis;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;

public class ReloadableSqlSessionFactoryBean extends SqlSessionFactoryBean implements Reloadable{
	
	private SqlSessionFactory sqlSessionFactoryProxy;
	
	public void reload() throws Exception {
		super.afterPropertiesSet();
		
	}
	
	public SqlSessionFactory getObject() throws Exception {
		return this.sqlSessionFactoryProxy;
	}
	public Class<? extends SqlSessionFactory> getObjectType()
	{
	    return (this.sqlSessionFactoryProxy != null ? this.sqlSessionFactoryProxy.getClass() : SqlSessionFactory.class);
	}
	
	public void afterPropertiesSet() throws Exception {
		super.afterPropertiesSet();
		this.sqlSessionFactoryProxy = 	(SqlSessionFactory) Proxy.newProxyInstance(

                SqlSessionFactory.class.getClassLoader(),

                new Class[] { SqlSessionFactory.class },

                new InvocationHandler()
                {
                    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable
                    {
                        return method.invoke(getParentObject(), args);
                    }
                });
	}
	
    private Object getParentObject() throws Exception
    {
            return super.getObject();
    }

}
